//
//  NetworkEngine.m
//  Growth
//
//  Created by 洪波 陈 on 5/29/12.
//  Copyright (c) 2012 MEETStudio. All rights reserved.
//

#import "NetworkEngine.h"

#import "UploadManagerEngine.h"

@implementation NetworkEngine

@synthesize hostNetworkStatus;
@synthesize internetNetworkStatus;
@synthesize wifiNetworkStatus;

static NetworkEngine *_shareEngine = nil;

+ (NetworkEngine *)sharedNetworkEngine
{
	if (!_shareEngine) {
		_shareEngine = [[NetworkEngine alloc] init];
		[_shareEngine initReachability];
	}
	
	return _shareEngine;
}

- (BOOL)checkNetWorkStatusUnLoading
{
    return [self checkNetWorkStatusWith:NO];
}

- (BOOL)checkNetWorkStatusLoading
{
    return [self checkNetWorkStatusWith:YES];
}

- (BOOL)checkNetWorkStatusWith:(BOOL)showLoading
{
    if (_shareEngine.hostNetworkStatus == NotReachable) {
        if (showLoading) {
            [LoadingView showImage:[UIImage imageNamed:@"wangluo_iphone.png"] info:@"哎呀，网络好像有问题，请重试！" duration:DEFAULT_LOADING_TIME *1.2];
        }
        
        return NO;
    }
    else{
        return YES;
    }
}

- (void)initReachability
{
    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
    // method "reachabilityChanged" will be called. 
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
    
    //Change the host name here to change the server your monitoring
	hostReach = [[Reachability reachabilityWithHostName:BASEURL] retain];
	[hostReach startNotifier];
	[self updateInterfaceWithReachability: hostReach];
	
//    internetReach = [[Reachability reachabilityForInternetConnection] retain];
//	[internetReach startNotifier];
//	[self updateInterfaceWithReachability: internetReach];
//    
//    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
//	[wifiReach startNotifier];
//	[self updateInterfaceWithReachability: wifiReach];
}


- (void) configureReachability: (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    BOOL connectionRequired= [curReach connectionRequired];
    NSString* statusString= @"";
    switch (netStatus)
    {
        case NotReachable:
        {
            statusString = @"Access Not Available";
            //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...
            connectionRequired= NO;  
            
            if (hostNetworkStatus != netStatus) {
                [LoadingView showDurationMessageLoadingViewWith:DEFAULT_LOADING_TIME message:@"网络连接断开"];
            }
            
            break;
        }
            
        case ReachableViaWWAN:
        {
            statusString = @"Reachable WWAN";
            
            if (hostNetworkStatus != netStatus) {
               // [LoadingView showDurationMessageLoadingViewWith:DEFAULT_LOADING_TIME message:@"2G/3G网络"];
            }
            
            break;
        }
        case ReachableViaWiFi:
        {
            statusString= @"Reachable WiFi";
            
            if (hostNetworkStatus != netStatus) {
               // [LoadingView showDurationMessageLoadingViewWith:DEFAULT_LOADING_TIME message:@"Wifi网络"];
            }
            
            
            break;
        }
    }

    hostNetworkStatus = netStatus;
    [[UploadManagerEngine sharedUploadManagerEngine] networkStateChanged:hostNetworkStatus];
}

- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    if(curReach == hostReach)
	{
        [self configureReachability:curReach];
    }
	if(curReach == internetReach)
	{	
		[self configureReachability:curReach];
	}
	if(curReach == wifiReach)
	{	
        [self configureReachability:curReach];
	}
	
}

//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
	Reachability* curReach = [note object];
	NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
	[self updateInterfaceWithReachability: curReach];
}
@end
